<?php
namespace Admin\Controller;
use Think\Controller;
class QiYeHaoController extends Controller {
    private $accessToken;

    /**
      * 打开签到页面
      */ 
    function index($code=''){
        // if(empty($this->accessToken)){
            $this->accessToken=$this->getAccessToken();
        // }
        // if(!empty($code)){
            $weixinid=$this->getUserId($this->accessToken,$code);
            $this->assign('weixinid',$weixinid);
        // }
        //
        $row=M('sign','tlsys_','DB_REMOTE')->where(array('weixinid'=>$weixinid))->order('signintime desc')->limit('0,6')->select(); 
        $this->assign('rows',$row);       
        $lastSignTime=$row[0]['signouttime'];
        $this->assign('lastSignTime',$lastSignTime);

        $this->display();
    }

    /**
     * 签到事件
     */
    function sign($weixinid=''){
        $res=0;
        if(!empty($weixinid)){
            $sign=M('sign','tlsys_','DB_REMOTE');
            //今日0点的时间戳
            $zerodaytime=strtotime(date('Y-m-d',time()));
            //今日是否已打过卡
            $count=$sign->where(array('weixinid'=>$weixinid,'signintime'=>array('gt',$zerodaytime)))->count();
            if($count>0){//已经打过卡，就把signouttime时间修改下
                $signouttime=time();
                $res=$sign->where(array('weixinid'=>$weixinid,'signintime'=>array('gt',$zerodaytime)))->setField('signouttime',$signouttime);

                //判定下是否早退
                //系统配置中的每天签出的时间
                $cet=getConfig('class_end_time');
                //拼凑成 当天日期加上开始上课时间如：2017-04-10 08:30
                $classEndTime=strtotime(date('Y-m-d',time()).' '.$cet);
                if($signouttime<$classEndTime){
                    $earlyquit['weixinid']=$weixinid;
                    $earlyquit['signouttime']=$signouttime;
                    $tomStamp=$zerodaytime+24*60*60*1000;
                    M('tlsys_early_quit')->where("weixinid='{$weixinid}' and signouttime between {$zerodaytime} and {$tomStamp}")->delete();
                    M('tlsys_early_quit')->data($earlyquit)->add();

                }else{
                    //这个是在处理：提前签退的人，已经记录了早退，但最后签退时间是在正常时间范围内，所以要把之前记录的早退记录清除掉
                    $tomStamp=$zerodaytime+24*60*60*1000;
                    M('tlsys_early_quit')->where("weixinid='{$weixinid}' and signouttime between {$zerodaytime} and {$tomStamp}")->delete();
                }

            }else{//未打过卡，新增一条数据
                $data['weixinid']=$weixinid;
                $data['signintime']=time();
                // $data['signouttime']=time();
                $res=$sign->data($data)->add();

                //判定下是否迟到
                //系统配置中的每天上课开始时间
                $cbt=getConfig('class_begin_time');
                //拼凑成 当天日期加上开始上课时间如：2017-04-10 08:30
                $classBeginTime=strtotime(date('Y-m-d',time()).' '.$cbt);
                if($data['signintime']>$classBeginTime){
                    $delay['weixinid']=$weixinid;
                    $delay['signintime']=$data['signintime'];
                    M('tlsys_delay')->data($delay)->add();
                }

            }
            $row=$sign->where(array('weixinid'=>$weixinid,'signintime'=>array('gt',$zerodaytime)))->find();
        }
        $this->ajaxReturn(array('res'=>$res,'row'=>$row,'count'=>$count));
    }

    function test(){
        echo urlencode('http://www.ittaoli.com/taoli_sys?m=Admin&c=QiYeHao&a=absent');
        // echo time().'<br/>';
        // $today=date('Y/m/d',time());
        // echo $today;
        // echo strtotime(date('Y-m-d',time()));
    }
    
    /**
     * 获得微信的accessToken
     */
    function getAccessToken(){
        $url='https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid='.C('CORPID').'&corpsecret='.C('SECRET');
        $result=getCurl($url);
        $result=json_decode($result);
        $accessToken=$result->access_token;
        return $accessToken;
    }

    /**
     * 通过accesstoken获得用户的微信ID
     */
    function getUserId($accessToken,$code){
        $url_uid='https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token='.$accessToken.'&code='.$code;
        $user=getCurl($url_uid);
        $user=json_decode($user);
        return $user->UserId;
    }

}